/**
 * get District and Village
 * @param mode
 * @return
 */
function getAddress(mode){
$(function(){
	if(mode == 'new')
	{
    	$("tr#district").hide();
    	$("tr#village").hide();
    	$("select#rtype").hide();
   		$("#sub-type").hide();
   		if($("select#category").val() != 0)
   		{
   			$("select#rtype").show();
   		}
   		if($("select#city_id").val() != 0)
   		{
   			$("tr#district").show();
   			if($("select#district_id").val() != 0)
   				$("tr#village").show();
   		}
	}
    else
    { 
    	if($("select#city_id").val() != 0)
    		$("tr#district").show();
    	else
    		$("tr#district").hide();
    	if($("select#district_id").val() != 0)
    		$("tr#village").show();
    	else
    		$("tr#village").hide();
    	
    	$("select#rtype").show();
   		$("#sub-type").show();
    }
	
	$("select#city_id").change(function()
	{   
		$.getJSON("/en/place/ajax/get-district",
				{
					city_id: $(this).val(), 
					ajax: 'true'
				}, 
                function(json){
                    var options = '';
                    for (var i = 0; i < json.length; i++) {
                           options += '<option value="' + json[i].id + '">' + json[i].name + '</option>';
                    }
                    if($("select#city_id").val() == 0)
                    {
                    	$("select#district_id option").removeAttr("selected");
                    	$("tr#district").hide();
                   		$("select#village_id option").removeAttr("selected");
                   		$("tr#village").hide();
                    }
                    else
                    {
                    	$("tr#district").show();
                    	$("select#village_id option").removeAttr("selected");
                    	$("tr#village").hide();
                    }
                   	$("select#district_id").html(options);
             })
     });
	$("select#district_id").change(function()
			{   
			$.getJSON("/en/place/ajax/get-village",
				{
					district_id: $(this).val(), 
					ajax: 'true'
				}, 
				function(json){    
					var options = '';
					for (var i = 0; i < json.length; i++) {
						options += '<option value="' + json[i].id + '">' + json[i].name + '</option>';
					}
					if($("select#district_id").val() == 0)
					{
						$("select#village_id option").removeAttr("selected");
						$("tr#village").hide();
					}
					else
					{
						$("tr#village").show();
					}
					$("select#village_id").html(options);
					
				})
				
	});
});
}

function updateCategoryCheckbox()
{
    //parent checkboxes
	$("div.category_wrapper > input.parent_category_checkbox").click(function(){
		if ($(this).is(':checked'))
		{
			//display the children_wrapper 
            //check if it has children before showing
            $div = $(this).siblings("div.children_wrapper");
            if ($div.find('input').size() > 0)
                $div.show();
		}
		else //uncheck children
		{
			$(this).siblings("div.children_wrapper").find("input").attr('checked', false);
			$(this).siblings("div.children_wrapper").fadeOut(300);	
		}
	});   
}

//get rtype
function getCategoryType(mode){
	
$(function() {  
     $("select#category").change(function()
		{   
    	 	if(mode != "search")
    	 	{
				 if($("select#category").val() == 0 )
				 {
					$("#sub-type" + " INPUT[@name=" + "type[]" + "][type='checkbox']").attr('checked', false);
					$("select#rtype").hide();
				 }
			     else
			     {
		     		$("#sub-type" + " INPUT[@name=" + "type[]" + "][type='checkbox']").attr('checked', false);
			     	$("select#rtype").show();
			     }
    	 	}
    	 	
			$.getJSON("/en/place/Ajax/get-root-type",
				{
					category: $(this).val(), 
					ajax: 'true'
				}, 
                function(json){  
                    var options = '';
                    for (var i = 0; i < json.length; i++) {
                           options += '<option value="' + json[i].id + '">' + json[i].name + '</option>';
                    }
                   	$("select#rtype").html(options);
		           	$("div.category_wrapper").hide(); 
             });
     });
	
     //if(mode == 'search' || mode == 'new')
     {
     $("select#rtype").change(function()
		{   
			if($("select#rtype").val() == 0 || $("select#rtype").val() == $("select#category").val())
			{
                $("#sub-type").hide();
			}
            else
            {
				$("select#rtype").show();            	
            	$("#sub-type").show();
            }
            	
			$.getJSON("/en/place/ajax/get-type",
			{
				rtype: $(this).val(), 
				ajax: 'true',
			}, 
            function(json){
                var options = '';
                rtype = $("select#rtype").val();
                for (var i = 0; i < json.length; i++) 
                {
                	if(json[i].pid == rtype) //parent categories
                	{
                		options +='<div class="category_wrapper"><input type="checkbox" class="parent_category_checkbox" value="' + json[i].id + '" name = "type[]">' + json[i].name  + 
                           			   '<br/>' + '</input>' ;
						//children_wrapper div
						options +='<div class="children_wrapper" style="display:none" >'; 	
                		for(var j = 0; j < json.length; j++) //children
                		{
                			if(json[j].pid == json[i].id )
                			{
                               	 options +='<input class="children_category_checkbox" type="checkbox" name = "type[]" value="' + json[j].id + '">' + json[j].name + '</input>&nbsp&nbsp&nbsp';
                			}
                		}
                		options += '</div></div>';
                    }
                }
                
                $("#sub-type").html(options);
                updateCategoryCheckbox();
                if (populate_category_checkboxes)
                {
					if(mode == "edit" || mode == "search" || mode == "new")
	                	populate_category_checkboxes();
                }
          });
     });
     }
});
}

$(function(){
	//check value
	$("#form_id").submit(function(){
	    if ($("#fee").val() > $("#to_fee").val() || $("#built_year").val() > $("#to_built_year").val())
	    {
	        alert("wrong fee range!");
	        return false;
	    }
	    else
	        return true;
	});
	
	//jquery slideUp
	$("fieldset.expansible legend").click(function () {
	      if ($(this).siblings().is(":hidden")) {
	        $(this).siblings().show("slow").end().removeClass("toggle-close").addClass("toggle-open").parent().removeClass("noborder");
	      } else { //close
	        $(this).siblings().slideUp().end().removeClass("toggle-open").addClass("toggle-close").parent().addClass("noborder");
	      }
	    });
	 $("fieldset.expansible legend").addClass("toggle-open");
	
	// changeStaytus
	$("select#bulk").change(function(){	
		if($(this).val() == 'status')
			$("#changeStatus").css("display","inline");
		else
			$("#changeStatus").css("display","none");
	});
	
	//delete one place and article
	$("a[name^=place-delete]").click(function(e){
		id = this.name.split('-')[2];
		$.getJSON("/en/place/ajax/delete-place", {'id': id},
				function(json) {
			if (json.success == 1){
				$("#place-row-" + id).fadeOut(2000); //2 seconds
			}
			else
			{
				alert('Not allowed to delete this user information. Check if your session expired');
				$("#delete-error-" + id).show().html("Could not delete");
				return;
			}
		});
		return false;
	});
	
	$("a[name^=set-status-deleted]").click(function(e){
		id = this.name.split('-')[3];
		$.getJSON("/en/place/ajax/status-deleted", {'id': id},
				function(json){
					if (json.success == 1){
						$("#place-row-" + id).fadeOut(2000);
					}
					else
					{
						alert('Could not set status deleted !!!');
						$("#delete-error-" + id).show().html("Could not set status deleted");
						return;
					}
		});
	});
});

			
	//delete bulk (places + article)
	function bulkModule(formId, mRowId, mStatus, mClass){
		bFormId = "#" + formId;
		bRowId = "#" + mRowId;
		bStatus = "#" + mStatus;
		bIdClass = "." + mClass;
		$(bFormId).ajaxForm({
				dataType:  'json',
				success: function(json){
				if(json.bulkaction == 'delete')
				{        
					if(json.success != 1)
					{
						alert('Not allowed to delete this user information. Check if your session expired');
						return;
					}
					$.each(json.ids, function(i,id){
						$(bRowId + id).css('background-color','red').fadeOut(2000).remove();
						//console.log("Removing place-box-" + id);        
					});
				}
				else if(json.success == '1'){
					if(json.bulkaction == 'status')
					{
						if(json.status == 'deleted' || json.status == 'spam'){
							$.each(json.ids, function(i,id){
								$(bRowId + id).css('background-color','blue').fadeOut(2000);
								$(bIdClass + ":checkbox:checked").click();
							});
						}
						else
						{
							$.each(json.ids, function(i,id){
								$(bIdClass + ":checkbox:checked").click();
								$(bStatus + id).html(json.status);
							});
						}
					}
					else
						alert('fall');
				}
				//set list null
				json.ids = null;
			}
		});
	}
			



